package com.yyw.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyw.bean.Shares;
import com.yyw.bean.User;
import com.yyw.dao.SharesDao;
import com.yyw.util.MyContext;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "分享资料API")
@RestController
@RequestMapping("/user")
@Slf4j
public class SharesController {

    @Autowired
    private SharesDao sharesDao;

    // 教师删除指定id的资料
    @CrossOrigin
    @ApiOperation(value = "教师删除指定id的资料", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "资料id", required = true) })
    @PostMapping("/deleteSharesById")
    public Result deleteSharesById(@RequestParam("id") Long id) {
        if (id == null) {
            return ResultFactory.notice("数据不合法");
        }
        int i = sharesDao.deleteShares(id);
        if (i > 0) {
            return ResultFactory.success("删除成功");
        } else {
            return ResultFactory.failure("删除失败");
        }
    }

    // 教师上传资料
    @CrossOrigin
    @ApiOperation(value = "教师上传资料", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "classid", value = "班级id", required = true),
            @ApiImplicitParam(name = "title", value = "标题", required = true),
            @ApiImplicitParam(name = "content", value = "内容", required = true) })
    @PostMapping("/saveShares1")
    public Result saveShares1(@RequestParam("classid") Long classid, @RequestParam("title") String title,
            @RequestParam("content") String content) {
        if (classid == null || StringUtils.isEmpty(title) || StringUtils.isEmpty(content)) {
            return ResultFactory.notice("数据不合法");
        }
        Shares shares = new Shares(null, classid, MyContext.getSessionUser().getId(), title, content, null, null);
        int i = sharesDao.saveShares(shares);
        if (i > 0) {
            return ResultFactory.success("保存成功");
        } else {
            return ResultFactory.failure("保存失败");
        }
    }

    // 学生上传资料
    @CrossOrigin
    @ApiOperation(value = "学生上传资料", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "title", value = "标题", required = true),
            @ApiImplicitParam(name = "content", value = "内容", required = true) })
    @PostMapping("/saveShares")
    public Result saveShares(@RequestParam("title") String title, @RequestParam("content") String content) {
        if (StringUtils.isEmpty(title) || StringUtils.isEmpty(content)) {
            return ResultFactory.notice("数据不合法");
        }

        User user = MyContext.getSessionUser();
        if (user.getClassid() == null) {
            return ResultFactory.notice("未加入班级");
        }

        Shares shares = new Shares(null, user.getClassid(), user.getId(), title, content, null, null);
        int i = sharesDao.saveShares(shares);
        if (i > 0) {
            return ResultFactory.success("保存成功");
        } else {
            return ResultFactory.failure("保存失败");
        }

    }

    @CrossOrigin
    @ApiOperation(value = "获取指定分享资料的详细信息", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "分享资料id", required = true) })
    @GetMapping("/getSharesById")
    public Result getSharesById(@RequestParam("id") Long id) {
        if (id == null) {
            return ResultFactory.notice("未加入班级");
        }

        Shares shares = sharesDao.getSharesById(id);
        return ResultFactory.success(shares);
    }

    @CrossOrigin
    @ApiOperation(value = "教师获取指定班级分享资料", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "classid", value = "班级id") })
    @GetMapping("/getClassSharesByClassId")
    public Result getClassSharesByClassId(@RequestParam("classid") Long classid) {
        if (classid == null) {
            return ResultFactory.notice("数据不合法");
        }
        List<Shares> list = sharesDao.getSharesByClassId(classid);
        return ResultFactory.success(list);
    }

    @CrossOrigin
    @ApiOperation(value = "教师获取指定班级分享资料，分页版", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "classid", value = "班级id"),
            @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getClassSharesByClassIdPages")
    public Result getClassSharesByClassIdPages(@RequestParam("classid") Long classid,
            @RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum) {
        if (classid == null) {
            return ResultFactory.notice("数据不合法");
        }

        if (pageNo < 1 || pageNum < 1) {
            return ResultFactory.notice("数据不合法");
        }

        PageHelper.startPage(pageNo, pageNum);
        List<Shares> list = sharesDao.getSharesByClassId(classid);
        PageInfo<Shares> pageInfo = new PageInfo<>(list);
        return ResultFactory.success(pageInfo);
    }

    @CrossOrigin
    @ApiOperation(value = "学生获取当前班级分享资料", httpMethod = "GET")
    @GetMapping("/getClassShares")
    public Result getClassShares() {
        Long classid = MyContext.getSessionUser().getClassid();
        if (classid == null) {
            return ResultFactory.notice("未加入班级");
        }
        List<Shares> list = sharesDao.getSharesByClassId(classid);
        return ResultFactory.success(list);
    }

    @CrossOrigin
    @ApiOperation(value = "学生获取当前班级分享资料，分页版", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getClassSharesPages")
    public Result getClassSharesPages(
            @RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum) {
        Long classid = MyContext.getSessionUser().getClassid();
        if (classid == null) {
            return ResultFactory.notice("未加入班级");
        }

        if (pageNo < 1 || pageNum < 1) {
            return ResultFactory.notice("数据不合法");
        }

        PageHelper.startPage(pageNo, pageNum);
        List<Shares> list = sharesDao.getSharesByClassId(classid);
        PageInfo<Shares> pageInfo = new PageInfo<>(list);
        return ResultFactory.success(pageInfo);
    }
}
